package com.famid.flashcards.data;

import java.util.ArrayList;
import java.util.List;

import com.famid.flashcards.data.Card.KnownStatus;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class CardStackDAOImpl implements CardStackDAO {
	
	private static final String TAG = CardStackDAOImpl.class.getSimpleName();
	private SQLiteDatabase database;
	private CardDBHelper dbHelper;
	private String[] allColumns = { CardDBHelper.COLUMN_ID, CardDBHelper.COLUMN_TITLE };
	private CardsDAO cardDao;
	
	public CardStackDAOImpl(Context context) {
		dbHelper = new CardDBHelper(context);
		cardDao = new CardsDAOImpl(context);
	}
	
	@Override
	public void open() throws SQLException {
		database = dbHelper.getWritableDatabase();
	}
	
	@Override
	public void close() {
		dbHelper.close();
	}

	@Override
	public CardStack createCardStack(CardStack cardStack) {
		ContentValues values = new ContentValues();
		values.put(CardDBHelper.COLUMN_TITLE, cardStack.getTitle());
		
		Log.d(TAG, "Insert new stack, title: "+ cardStack.getTitle());
		long insertId = database.insert(CardDBHelper.TABLE_STACK, null,	values);
		
		// To show how to query
		Cursor cursor = database.query(CardDBHelper.TABLE_STACK,
				allColumns, CardDBHelper.COLUMN_ID + " = " + insertId, null,
				null, null, null);
		cursor.moveToFirst();
		CardStack ret = cursorToCardStack(cursor);
		return ret;
	}

	@Override
	public void deleteCardStack(CardStack cardStack) {
		long id = cardStack.getId();
		cardDao.open();
		cardDao.deleteForStack(id);
		cardDao.close();
		Log.d(TAG, "Delete stack with id: " + id);
		database.delete(CardDBHelper.TABLE_STACK, CardDBHelper.COLUMN_ID + " = " + id, null);
	}

	@Override
	public List<CardStack> getAllCardStacks() {
		List<CardStack> stacks = new ArrayList<CardStack>();
		Log.d(TAG, "Get all cards.");
		Cursor cursor = database.query(CardDBHelper.TABLE_STACK, allColumns, null, null, null, null, null);
		cursor.moveToFirst();
		cardDao.open();
		while (!cursor.isAfterLast()) {
			CardStack stack = cursorToCardStack(cursor);
			
			List<Card> cards = cardDao.getAllCardsForStack(stack.getId());
			int known = 0;
			for(Card c : cards) {
				if(c.getStatus() == KnownStatus.KNOWN)
					known++;
			}
			double knownPercent = cards.size()>0?100 * known / cards.size():0;
			stack.setKnownPercent(knownPercent);
			stack.setCardSize(cards.size());
			
			stacks.add(stack);
			cursor.moveToNext();
		}
		cardDao.close();
		// Make sure to close the cursor
		cursor.close();
		Log.d(TAG, "Result size: "+stacks.size());
		return stacks;
		
	}
	
	private CardStack cursorToCardStack(Cursor cursor) {
		long id = cursor.getLong(0);
		String title = cursor.getString(1);
		return new CardStack(id, title);		
	}

	@Override
	public CardStack get(long id) {
		Cursor cursor = database.query(CardDBHelper.TABLE_STACK,
				allColumns, CardDBHelper.COLUMN_ID + " = " + id, null,
				null, null, null);
		cursor.moveToFirst();
		CardStack ret = cursorToCardStack(cursor);
		cardDao.open();
		ret.setCardSize(cardDao.countByStack(id));
		cardDao.close();
		return ret;
	}

	@Override
	public CardStack update(CardStack stack) {
		Log.d(TAG, "Update stack id: "+stack.getId());
		ContentValues values = new ContentValues();
		values.put(CardDBHelper.COLUMN_TITLE, stack.getTitle());
		
		database.update(CardDBHelper.TABLE_STACK, values, CardDBHelper.COLUMN_ID + " = " + stack.getId(), null);
		return get(stack.getId());
	}
}
